﻿using System;
using System.Reflection;
using System.Collections.Generic;
using NLog;
using dlCentral.Utils;
using dlCentral.PluginHandlers;
using System.Xml;
using System.Net;
using System.Text.RegularExpressions;

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;

namespace dlCentral.TorrentHandlers {
    internal class MonovaHandler : TorrentHandler {
        private static Logger logger = LogManager.GetCurrentClassLogger();
        string size = "";
        string seeds = "";
        string peers = "";
        

        // The torrent ID for the plugin this handler implements support for.
        public override TorrentId ID
        {
            get { return TorrentId.MONOVA; }
            set { }
        }

        // Should return the name of the source Torrent providing the data.
        public override string TorrentName {
            get { return "Monova"; }
            set { }
        }

        private void GetTorrentDetails(string torurl, MediaDetail item)
        {
    //    http://www.monova.org/details-rss/3965609/sons of anarchy s03e07 widening gyre hdtv xvid fqm.html
//        http://www.monova.org/download-rss/3965609/2010-10-20/901625c891c2fce13b5ab573bda33009ae8f3590/210f1ed57dd2183cf5a6b96aef1ca396/sons of anarchy s03e07 widening gyre hdtv xvid fqm.torrent
            try
            {
                string spub = "";
                string ssize = "";
                string sseeds = "";
                string speers = "";

                string str = get(torurl);

                int start = str.IndexOf(">Tracker");
                int len = 160;
                string str1 = str.Substring(start, len);
                start = str1.IndexOf("font>") + 6;
                len = str1.IndexOf("<br") - start;
                spub = str1.Substring(start, len);
                string lang = "";
                if (spub == "(Publiekelijk)") lang = "nl";
                if (spub == "(Public)") lang = "en";
                if (spub == "(Öffentlich)") lang = "de";
                if (spub == "(Publique)") lang = "fr";
                if (spub == "(Yleinen)") lang = "it";
                if (spub == "(Publico)") lang = "pt";
                if (spub == "(Público)") lang = "sp";
                if (spub == "(Publiczny)") lang = "pl";
                if (spub == "(Offentlig)") lang = "no";
                if (lang == "nl")
                {
                    ssize = ">Totale grootte:</";
                    sseeds = "seeds, <";
                    speers = "peers<";
                }
                if (lang == "en")
                {
                    ssize = ">Total size:</";
                    sseeds = "seeds, <";
                    speers = "peers<";
                }
                if (lang == "de")
                {
                    ssize = ">Gesamte Grösse:</";
                    sseeds = "Seeds, <";
                    speers = "Peers<";
                }
                if (lang == "fr")
                {
                    ssize = ">Taille totale:</";
                    sseeds = "Source, <";
                    speers = "Téléchargeurs<";
                }
                if (lang == "it")
                {
                    ssize = ">Koko:</";
                    sseeds = "seediä, <";
                    speers = "peeriä<";
                }

                if (lang == "pt")
                {
                    ssize = ">Tamanho total:</";
                    sseeds = "Seeds, <";
                    speers = "Peers<";
                }
                if (lang == "sp")
                {
                    ssize = ">Tamaño total:</";
                    sseeds = "seeds, <";
                    speers = "peers<";
                }
                if (lang == "pl")
                {
                    ssize = ">Łączny rozmiar:</";
                    sseeds = "seedów, <";
                    speers = "peerów<";
                }
                if (lang == "no")
                {
                    ssize = ">Total størrelse:</";
                    sseeds = "seeds, <";
                    speers = "peers<";
                }

                if (lang == "") return;


                start = str.IndexOf("downloadbox");
                len = str.IndexOf(".torrent")-start+40;
                str1 = str.Substring(start, len);
                start = str1.IndexOf("href=") + 6;
                len = str1.IndexOf("rel=") - 2 - start;
                str1 = str1.Substring(start, len);
                item.TorrentURL = str1;

                if (str.IndexOf(ssize) >0)
                   start = str.IndexOf(ssize)+23;
                else
                   start = str.IndexOf(ssize) + 23;
                
                len = 80;
                str1 = str.Substring(start, len);
                start = str1.IndexOf(">") + 1;
                len = str1.IndexOf("<br") - start;
                str1 = str1.Substring(start, len);
                size = str1;

                start = str.IndexOf(sseeds)-22;
                len = 80;
                str1 = str.Substring(start, len);
                start = str1.IndexOf(">") + 1;
                len = str1.IndexOf("</") - start;
                str1 = str1.Substring(start, len);
                seeds = str1;

                start = str.IndexOf(speers)-22;
                len = 80;
                str1 = str.Substring(start, len);
                start = str1.IndexOf(">") + 1;
                len = str1.IndexOf("</") - start;
                str1 = str1.Substring(start, len);
                peers = str1;
            }
            catch { return; }
        }

        public override void GetDetails(List<MediaDetail> Results)
        {
            if (DLSType == SiteSearchType.TVSHOW) url = string.Format("http://www.monova.org/rss.php?type=search&term={0}", SearchTerm);
            if (DLSType == SiteSearchType.IMDb) url = "http://www.monova.org/rss.php?type=catname&id=1";

            JObject jobject = new JObject();
            string date = "";
            XmlNodeList xml = getXML(url);
            if (xml != null)
            {
            XmlNodeList Nodes = xml.Item(0).SelectNodes("item");
            if (Nodes != null)
                foreach (XmlNode currNode in Nodes)
                {
                    MediaDetail item = new MediaDetail();

                    foreach (XmlNode node in currNode.ChildNodes)
                    {
                        string value = node.InnerText;
                        switch (node.Name)
                        {
                            case "title":
                                item.Name = value;
                                break;
                            case "category":
                                Category = value;
                                break;
                            case "pubDate":
                                date = value;
                                break;
                            case "link":
                                GetTorrentDetails(value, item);
                                break;
                        }
                    }


                    item.Date = DateTime.Parse(date);
                    item.SearchType = DLSType;
                    item.Site = Site;
                    item.TorrentInfo = size + " S/P : " + seeds + "/" + peers;
                    if (DLSType == SiteSearchType.TVSHOW) item.IsInfoChecked = true;
                    if (item.TorrentURL != null && item.TorrentURL != "") Results.Add(item);
                }
            }



        }

        protected override bool IsAvailable()
        {
            return true;
        }


    }
}
